package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.TalkBackDTO;
import com.jyh.blog.dto.TalkDTO;
import com.jyh.blog.service.TalkService;
import com.jyh.blog.strategy.context.UploadStrategyContext;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import com.jyh.blog.vo.TalkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.REMOVE;
import static com.jyh.blog.constant.OptTypeConst.SAVE_OR_UPDATE;
import static com.jyh.blog.enums.FilePathEnum.TALK;

@Api(tags = "说说管理")
@RestController
public class TalkController {
    @Autowired
    private TalkService talkService;
    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    /**
     * 查看首页说说
     * @return
     */
    @ApiOperation("查看首页说说")
    @GetMapping("home/talks")
    public Result<List<String>> talkHomeList(){
        return Result.ok(talkService.talkHomeList());
    }

    /**
     * 前台说说分页列表
     * @return
     */
    @ApiOperation(value = "分页查询首页说说")
    @GetMapping("talks")
    public PageResult<TalkDTO> talkList(){
        return talkService.talkList();
    }


    /**
     * 后台说说分页列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "分页查询后台说说")
    @PostMapping("admin/talks")
    public Result<PageResult<TalkBackDTO>> talkBackList(@RequestBody ConditionVO conditionVO) {
        return Result.ok(talkService.talkBackList(conditionVO));
    }

    /**
     * 根据id获取说说
     * @param talkId
     * @return
     */
    @ApiOperation(value = "根据id获取首页说说")
    @GetMapping("talk/{talkId}")
    public Result<TalkDTO> getTalkById(@PathVariable String talkId) {
        return Result.ok(talkService.getTalkById(talkId));
    }

    /**
     * 添加或修改说说
     * @param talkVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改说说")
    @PostMapping("/admin/talk")
    public Result saveOrUpdateTalk(@Valid @RequestBody TalkVO talkVO) {
        talkService.saveOrUpdateTalk(talkVO);
        return Result.ok();
    }

    /**
     * 根据id查询说说（后台）
     * @param talkId
     * @return
     */
    @ApiOperation(value = "根据id查询后台说说")
    @GetMapping("admin/talk/{talkId}")
    public Result<TalkBackDTO> getTalkBackById(@PathVariable String talkId) {
        return Result.ok(talkService.getTalkBackById(talkId));
    }

    /**
     * 彻底删除
     * @param talkId
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除说说")
    @DeleteMapping("admin/talk/{talkId}")
    public Result deleteTalk(@PathVariable String talkId) {
        talkService.deleteTalk(talkId);
        return Result.ok();
    }

    /**
     * 说说上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "说说上传图片")
    @PostMapping("admin/talk/images")
    public Result<String> uploadImg(MultipartFile file){
        return Result.ok(uploadStrategyContext.executeUploadStrategy(file,TALK.getPath()));
    }

    /**
     * 说说点赞
     * @param talkId
     * @return
     */
    @ApiOperation(value = "说说点赞")
    @PostMapping("talk/{talkId}/like")
    public Result talkLike(@PathVariable("talkId") String talkId){
        talkService.talkLike(talkId);
        return Result.ok();
    }

}
